#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
const int PageSize = 100; // Size of each page frame
const int PageCount = 5;  // Number of page frames

int pageTable[15];
int main(){
    // 打开内存映射文件对象
    HANDLE hMapFile=OpenFileMapping(
        FILE_MAP_ALL_ACCESS,
        FALSE,
        TEXT("memory")
    );
    if(hMapFile==NULL){
        cerr<<"Could not open file mapping object: "<<GetLastError()<<endl;
        return 0;
    }
    // 映射缓冲区视图
    char* pBuf = static_cast<char*>(MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, PageSize * PageCount));
    if (pBuf == NULL) {
        cerr<<"Could not map view of file: "<<GetLastError()<<endl;
        CloseHandle(hMapFile);
        return 0;
    }


    //连接管道
    if(!WaitNamedPipe(TEXT("\\\\.\\Pipe\\pipe"),NMPWAIT_WAIT_FOREVER)){
        cerr<<"Connect hPipe failed"<<endl;
        return 0;
    }
    //打开管道
    HANDLE hPipe=CreateFile(
        TEXT("\\\\.\\Pipe\\pipe"),
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );
    if(hPipe==INVALID_HANDLE_VALUE){
        cout<<"Open hPipe failed"<<endl;
    }


    //连接管道
    if(!WaitNamedPipe(TEXT("\\\\.\\Pipe\\memoryPipe"),NMPWAIT_WAIT_FOREVER)){
        cerr<<"Connect memoryPipe failed"<<endl;
        return 0;
    }
    //打开管道
    HANDLE memoryPipe=CreateFile(
        TEXT("\\\\.\\Pipe\\memoryPipe"),
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );
    if(memoryPipe==INVALID_HANDLE_VALUE){
        cout<<"Open memoryPipe failed"<<endl;
    }



    CHAR Buffer[10][11]={"3","2","1","4","5","6","10","9","8","7"};
    CHAR contents[101];
    CHAR pageNumber[101];
    memset(contents,'\0',sizeof(contents));
    memset(pageNumber,'\0',sizeof(pageNumber));
    DWORD dwNumberOfBytesWritten=0;
    DWORD dwNumberOfBytesRead=0;
    for(int i=0;i<10;i++){
        //向disk发送需要的逻辑块号
        WriteFile(hPipe,Buffer[i],strlen(Buffer[i]),&dwNumberOfBytesWritten,NULL);
        //从disk中读取到需要的块的内容
        ReadFile(hPipe,contents,sizeof(contents),&dwNumberOfBytesRead,NULL);


        //向memory发送请求
        WriteFile(memoryPipe,"request",strlen("request"),&dwNumberOfBytesWritten,NULL);
        //从memory接收到空白页框号
        ReadFile(memoryPipe,pageNumber,sizeof(pageNumber),&dwNumberOfBytesRead,NULL);
        pageTable[stoi(Buffer[i])]=stoi(pageNumber);
        //向页框中写入
        char* writePage = pBuf + 100 * stoi(pageNumber);
        strcpy(writePage, contents);
        cout<<"memory content : "<<endl;
        for(int i=0;i<5;i++){
            cout<<"Page "<<i<<" : "<<pBuf+i*100<<endl;
        }
        cout<<endl;
        
        memset(contents,'\0',sizeof(contents));
        memset(pageNumber,'\0',sizeof(pageNumber));
    }
    DisconnectNamedPipe(hPipe);
	CloseHandle(hPipe);//关闭管道
    DisconnectNamedPipe(memoryPipe);
	CloseHandle(memoryPipe);//关闭管道
	system("pause");
    return 0;
}